package com.yy.ant.chat.service.impl;

import com.yy.ant.chat.entity.Flock;
import com.yy.ant.chat.entity.Flockrefuser;
import com.yy.ant.chat.mapper.FlockMapper;
import com.yy.ant.chat.mapper.FlockrefuserMapper;
import com.yy.ant.chat.service.IFlockService;
import com.yy.ant.sys.entity.User;
import com.yy.ant.sys.mapper.UserMapper;
import com.yy.model.ResultMsg;

import javassist.tools.framedump;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 群组 服务实现类
 * </p>
 *
 * @author liyingying
 * @since 2020-01-06
 */
@Service
public class FlockServiceImpl extends ServiceImpl<FlockMapper, Flock> implements IFlockService {

	@Autowired
	private FlockMapper flockMapper;
	@Autowired
	private FlockrefuserMapper flockrefuserMapper;
	@Autowired
	private UserMapper userMapper;
	

	@Value("${yy.touxiang}")
	private String url;
	/**
	 * 查询我的分组
	 */
	@Override
	public List<Flock> getFlocks(Long id,String url) {
		List<Flock> flocks = new ArrayList<Flock>();
		List<Flockrefuser> refUser = flockrefuserMapper.selectList(new QueryWrapper<Flockrefuser>().eq("uid", id));
		List<Integer> list=new ArrayList<Integer>();
		for (Flockrefuser o:refUser) {
			list.add(o.getFid());
		}
		if (list!=null&&list.size()>0) {
			flocks=flockMapper.selectBatchIds(list);
		}
		//为群设置头像
		for (Flock o:flocks) {
			o.setAvatar(url+o.getAvatar()+".jpg");
		}
		return flocks;
	}

	/**
	 * 根据群ID找到群成员
	 */
	@Override
	public ResultMsg getMembers(String id,String url) {
		List<Flockrefuser> list = flockrefuserMapper.selectList(new QueryWrapper<Flockrefuser>().eq("fid", id));
		List<User> users=  new ArrayList<User>();
		for(Flockrefuser u : list){
			User ser = userMapper.selectById(u.getUid());
			//为用户设置头像
			if (StringUtils.isNotBlank(ser.getAvatar())) {
				ser.setAvatar(url+ser.getAvatar()+".jpg");
			}
			users.add(ser);
		}
		ResultMsg resultMsg=new ResultMsg();
		Map<String,Object> war = new HashMap<String,Object>();
        war.put("list", users);
		resultMsg.setData(war);
		return resultMsg;
	}

	@Override
	public List<User> getMember(String id, String url) {
		List<Flockrefuser> list = flockrefuserMapper.selectList(new QueryWrapper<Flockrefuser>().eq("fid", id));
		List<User> users=  new ArrayList<User>();
		for(Flockrefuser u : list){
			User ser = userMapper.selectById(u.getUid());
			//为用户设置头像
			ser.setAvatar(url+ser.getAvatar()+".jpg");
			users.add(ser);
		}
		return users;
	}

	@Override
	public ResultMsg getUserFlock(String name, String string2, String yema) {
		ResultMsg resultMsg=new ResultMsg();
		List<HashMap<String, Object>> listsList=new ArrayList<HashMap<String,Object>>();
		Long countLong=0L;
		if (StringUtils.equals(string2, "0")) {
			//人
			QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
			queryWrapper.like("username", name);
			IPage<User> iPage = userMapper.selectPage(new Page<>(Integer.valueOf(yema), Integer.valueOf(12)), queryWrapper);
			List<User> list=iPage.getRecords();
			countLong=iPage.getTotal();
			for (User o:list) {
				HashMap<String, Object> hashMap=new HashMap<String, Object>();
				hashMap.put("id", o.getId());
				hashMap.put("name", o.getUsername());
				//头像
				hashMap.put("avatar", url+o.getAvatar()+".jpg");
				listsList.add(hashMap);
			}
			
		}else {
			//群
			QueryWrapper<Flock> queryWrapper = new QueryWrapper<Flock>();
			queryWrapper.like("groupname", name);
			IPage<Flock> iPage = flockMapper.selectPage(new Page<>(Integer.valueOf(yema), Integer.valueOf(12)), queryWrapper);
			List<Flock> list=iPage.getRecords();
			countLong=iPage.getTotal();
			for (Flock o:list) {
				HashMap<String, Object> hashMap=new HashMap<String, Object>();
				hashMap.put("id", o.getId());
				hashMap.put("name", o.getGroupname());
				//头像
				hashMap.put("avatar", url+o.getAvatar()+".jpg");
				listsList.add(hashMap);
			}
			
		}
		resultMsg.setData(listsList);
		resultMsg.setMsg(String.valueOf(countLong));
		return resultMsg;
	}

	

}
